package com.pllandxzx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pll.pojo.UserReview;
import com.pllandxzx.dto.ResponseDTO;
import com.pllandxzx.mapper.MerchantMapper;
import com.pllandxzx.mapper.UserReviewMapper;
import com.pllandxzx.service.MerchantService;
import com.pllandxzx.vo.CheckVo;
import com.pllandxzx.vo.MerchantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName：MerchantServiceImpl
 * @Author: 阿⭐
 * @Date: 2025/6/12 11:49
 * @Description: TODO
 */
@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private UserReviewMapper userReviewMapper;


    @Override
    public ResponseDTO getMerchantList(MerchantVo merchantVo) {
        // 分页对象
        Page<MerchantVo> page = new Page<>(merchantVo.getPageNo(), merchantVo.getPageSize());
        System.out.println(merchantVo.getMerAddress());
        return ResponseDTO.Success(merchantMapper.getMerchantList(page,merchantVo.getMerAddress(),merchantVo.getLicense(),merchantVo.getCertification(),merchantVo.getMerName(),merchantVo.getMerPhone()));
    }

    @Override
    public ResponseDTO getMerchantDetail(long id) {
        return ResponseDTO.Success(merchantMapper.getMerchantDetail(id));
    }

    @Override
    public ResponseDTO getMerchantComments(long id) {
        return ResponseDTO.Success(merchantMapper.getMerchantComments(id));
    }

    @Override
    public ResponseDTO deleteMerchantComment(int reviewId) {
        return ResponseDTO.Success(200,"删除成功",userReviewMapper.delete(new LambdaQueryWrapper<UserReview>().eq(UserReview::getReviewId,reviewId)));
    }

    @Override
    public ResponseDTO resetMerchantPassword(int id) {
        return ResponseDTO.Success(merchantMapper.resetMerchantPassword(id));
    }

    @Override
    public ResponseDTO updateMerchantStatus(int id, String status) {
        return ResponseDTO.Success(merchantMapper.updateMerchantStatus(id,status));
    }

    @Override
    public ResponseDTO getCheckList(MerchantVo merchantVo) {
        // 分页对象
        Page<MerchantVo> page = new Page<>(merchantVo.getPageNo(), merchantVo.getPageSize());
        return ResponseDTO.Success(merchantMapper.getCheckList(page,merchantVo.getMerPhone(),merchantVo.getCheckResult()));
    }

    @Override
    public ResponseDTO auditMerchant(long id, CheckVo checkVo) {
        //修改审核表信息
        Integer i = merchantMapper.auditMerchant(id, checkVo.getCheckResult(), checkVo.getCheckNote(), checkVo.getCheckAdminId());
        if (i == null || i == 0){
            return ResponseDTO.Fail(1005,"审核失败");
        }
        //通过审核id获取当前审核的商家id
        int merchantId = merchantMapper.getMerchantIdByCheckId(id);
        //如果审核通过审核
        if (checkVo.getCheckResult().equals("1")){
            //更新商家认证状态为已认证
            merchantMapper.updateMerchantCertification(merchantId, 2);
            return ResponseDTO.Success(200,"审核成功");
        }
//        //如果审核不通过审核
//        if (checkVo.getCheckResult().equals("2")){
//            //更新商家认证状态为未认证
//            merchantMapper.updateMerchantCertification(merchantId,0);
//            return ResponseDTO.Success(200,"审核成功");
//        }
        return ResponseDTO.Fail(1005,"审核失败");
    }
}
